#coding:utf-8
from thrift.Thrift import TType, TMessageType, TException, TApplicationException
from thrift.protocol import TBinaryProtocol
from struct import pack, unpack

class JMTBinaryProtocol(TBinaryProtocol.TBinaryProtocol):

  VERSION_MASK = 0xffff0000
  VERSION_1 = 0x80010000

  def readAllIn(self):
    # buff = self.trans.readAll(1024)
    # print buff

    # return buff
    print "read version"
    buff = self.trans.readAll(4)
    val, = unpack('!i', buff)
    print val

    print "read name"
    buff = self.trans.readAll(4)
    val2, = unpack('!i', buff)
    buff = self.trans.readAll(val2)
    print buff

    print "read seqid"
    buff = self.trans.readAll(4)
    val, = unpack('!i', buff)
    print val

    print "read a byte"
    buff = self.trans.readAll(1)
    val4, = unpack('!b', buff)
    print val4

    # print "read a other byte"
    # buff = self.trans.readAll(2)
    # val, = unpack('!h', buff)
    # print val
    buff = self.trans.readAll(1)
    val4, = unpack('!b', buff)
    print val4

    print 'read int 32'
    buff = self.trans.readAll(4)
    val, = unpack('!i', buff)
    print val

    # buff = self.trans.readAll(1)
    # val5, = unpack('!b', buff)
    # print val5

    # buff = self.trans.readAll(4)
    # val6, = unpack('!I', buff)
    # print val6


    # buff = self.trans.readAll(4)
    # val7, = unpack('!I', buff)
    # print val7
    # buff = self.trans.readAll(val7)
    # print buff

    # buff = self.trans.readAll(4)
    # val8, = unpack('!I', buff)
    # print val8
    # buff = self.trans.readAll(val8)
    # print buff

    # # method
    # buff = self.trans.readAll(4)
    # val7, = unpack('!I', buff)
    # print val7
    # buff = self.trans.readAll(val7)
    # print buff
    # # balance
    # buff = self.trans.readAll(4)
    # val8, = unpack('!I', buff)
    # print val8
    # buff = self.trans.readAll(val8)
    # print buff

    # # version
    # buff = self.trans.readAll(4)
    # val9, = unpack('!I', buff)
    # print val9
    # buff = self.trans.readAll(val9)
    # print buff

    # # version
    # buff = self.trans.readAll(4)
    # val9, = unpack('!I', buff)
    # print val9
    # buff = self.trans.readAll(val9)
    # print buff



  # def readFieldBegin(self):
  #   type = self.readByte()
  #   if type == TType.STOP:
  #     return (None, type, 0)

  #   id = self.readI16()
  #   return (None, type, id)

  # def readString(self):
  #   var, len = self.readI32JM()
  #   str = self.trans.readAll(len)
  #   return str

  def readMessageBegin(self):
    self.readContext()
    sz = self.readI32()
    if sz < 0:
      version = sz & JMTBinaryProtocol.VERSION_MASK
      if version != JMTBinaryProtocol.VERSION_1:
        raise TProtocolException(
          type=TProtocolException.BAD_VERSION,
          message='Bad version in readMessageBegin: %d' % (sz))
      type = sz & JMTBinaryProtocol.TYPE_MASK
      name = self.readString()
      seqid = self.readI32()
    else:
      if self.strictRead:
        raise TProtocolException(type=TProtocolException.BAD_VERSION,
                                 message='No protocol version header')
      name = self.trans.readAll(sz)
      type = self.readByte()
      seqid = self.readI32()
    return (name, type, seqid)

  def readListBegin(self):
    etype = self.readByte()
    size = self.readI32()
    return (etype, size)


  def readContext(self):
    # print "read version"
    buff = self.trans.readAll(4)
    version, = unpack('!i', buff)


    # print "read name"
    buff = self.trans.readAll(4)
    val2, = unpack('!i', buff)
    buff = self.trans.readAll(val2)

    # print "read seqid"
    buff = self.trans.readAll(4)
    val, = unpack('!i', buff)
    # print val

    # print "read a byte"
    buff = self.trans.readAll(1)
    val4, = unpack('!b', buff)
    # print val4

    # print "read a other byte"
    buff = self.trans.readAll(1)
    val4, = unpack('!b', buff)
    # print val4

    # print 'read int 32'
    buff = self.trans.readAll(4)
    contextCount, = unpack('!i', buff)
    for i in xrange(0,contextCount):
      key = self.readString()
      value = self.readString()
      # print key, value
    self.readMapEnd()
    self.readMessageEnd()


  # def readMessageBeginJM(self):
  #   all_len = 0
  #   (len, sz) = self.readI32JM()
  #   if sz < 0:
  #     version = sz & TBinaryProtocol.VERSION_MASK
  #     if version != TBinaryProtocol.VERSION_1:
  #       raise TProtocolException(
  #         type=TProtocolException.BAD_VERSION,
  #         message='Bad version in readMessageBegin: %d' % (sz))
  #     type = sz & TBinaryProtocol.TYPE_MASK
  #     (len1, name) = self.readStringJM()
  #     (len2, seqid) = self.readI32JM()
  #     all_len = len + len1 + len2
  #   else:
  #     if self.strictRead:
  #       raise TProtocolException(type=TProtocolException.BAD_VERSION,
  #                                message='No protocol version header')
  #     name = self.trans.readAll(sz)
  #     (len1, type) = self.readByteJM()
  #     (len2, seqid) = self.readI32JM()
  #     all_len = len + sz + len1 + len2
  #   return (all_len, name, type, seqid)

  # def readMapBeginJM(self):
  #   (len1, ktype) = self.readByteJM()
  #   (len2, vtype) = self.readByteJM()
  #   (len3, size) = self.readI32()
  #   all_len = len1 + len2 + len3
  #   return (all_len, ktype, vtype, size)

  def writeMessageBeginJM(self, name, type, seqid):
    if self.strictWrite:
      return self.writeI32JM(JMTBinaryProtocol.VERSION_1 | type)+self.writeStringJM(name)+self.writeI32JM(seqid)
    else:
      return self.writeStringJM(name)+self.writeByteJM(type)+self.writeI32JM(seqid)

  def writeMapBeginJM(self, ktype, vtype, size):
    return self.writeByteJM(ktype)+self.writeByteJM(vtype)+self.writeI32JM(size)

  def readStringJM(self):
    (len, ) = self.readI32JM()
    str = self.trans.readAll(len)
    return (len, str)

  def readI32JM(self):
    buff = self.trans.readAll(4)
    val, = unpack('!I', buff)
    return (4, val)

  def readByteJM(self):
    buff = self.trans.readAll(1)
    val, = unpack('!b', buff)
    return (1, val)

  def writeI32JM(self, i32):
    buff = pack("!I", i32)
    self.trans.write(buff)
    return 4

  def writeStringJM(self, str):
    length = len(str)
    result = self.writeI32JM(length)
    if (length > 0):
      self.trans.write(str)
    return length

  def writeByteJM(self, byte):
    buff = pack("!b", byte)
    self.trans.write(buff)
    return 1

